<?php

namespace App\Models\Purchase;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class PurchasePrePayOrder extends Model
{
    protected $table = 'purchase_pre_pay_order';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];
    protected $casts = [
        'payment_info' => 'array',
    ];
    /**
     *获取列表 
     */
    public static function getList(Request $request, $cpid, $manger_id=0)
    {
        $query = self::where(['cpid' => $cpid, 'is_del' => 1])->orderBy('create_at', 'desc');
        if ($manger_id > 0) {
            $query->where('manger_id', $manger_id);
        }

        $audit_status = $request->audit_status;
        if (in_array($audit_status, [1, 2, 3, 4])) {
            $query->where('audit_status', $audit_status);
        }


        $pa_type = $request->pa_type;
        if (in_array($pa_type, [1, 2, 3,4])) {
            $query->where('pa_type', $pa_type);
        }


        $company_name = $request->company_name; //供应商名称
        if ($company_name != '') {
            $query->where('company_name', 'like', '%' . $company_name . '%');
        }

        $pa_contract_no = $request->pa_contract_no; //采购合同号
        if ($pa_contract_no != '') {
            $query->where('pa_contract_no', 'like', '%' . $pa_contract_no . '%');
        }
        $bank = $request->bank; //采购合同号
        if ($bank != '') {
            $query->where('bank', 'like', '%' . $bank . '%');
        }
            
        $start_time = strtotime($request->start_time)>0?strtotime($request->start_time):intval($request->start_time);
        $end_time = strtotime($request->end_time)>0?strtotime($request->end_time):intval($request->end_time);
        if ($start_time > 0 && $end_time > 0) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }


        $limit = $request->limit ?? 10;
        $list = $query->paginate($limit)->toArray();
        return paginateDataToArray($list);
    }
}